//
//  FQFaceCerificationViewController.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/24.
//

import UIKit

class FQFaceCerificationViewController: BaseViewController {
    var verificationToken : String = ""
    var nameAes : String = ""
    var idCardAes : String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarType = .light
    }
    
    override func initSubviews() {
        super.initSubviews()
        safeAreaBottomView.backgroundColor = .white
        
        let titleLb = QMUILabel(font: .mSFont(21), textColor: .color51)
        titleLb.text = "人脸识别认证"
        view.addSubview(titleLb)
        
        titleLb.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(NavigationContentTop + 32)
        }
        
        let subTitleLb = QMUILabel(font: .rSFont(13), textColor: .color149)
        subTitleLb.text = "用于身份认证，保障您的资金安全"
        view.addSubview(subTitleLb)
        
        subTitleLb.snp.makeConstraints { make in
            make.left.equalTo(titleLb)
            make.top.equalTo(titleLb.snp.bottom).offset(5)
        }
        
        let imageV = UIImageView(imageString: "icon_faceCerification")
        view.addSubview(imageV)
        
        imageV.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(subTitleLb.snp.bottom).offset(55)
        }
        
        let tipLb = QMUILabel(font: .mSFont(16), textColor: .color51)
        tipLb.text = "请根据指示完成人脸认证"
        view.addSubview(tipLb)
        
        tipLb.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(imageV.snp.bottom).offset(19)
        }
        
        let tipV = FQCerificationTipView(2)
        view.addSubview(tipV)
        
        tipV.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(tipLb.snp.bottom).offset(58)
        }
        
        let bottomView = FQAuthBottonView()
        bottomView.completionBtn.setTitle("点击认证", for: .normal)
        bottomView.completionBtn.isEnabled = true
        view.addSubview(bottomView)
        
        bottomView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(90)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
        
        bottomView.buttonBlock = { [unowned self] in
            self.doFaceAuth()
        }
    }
    
    func doFaceAuth() {
        let extParams : [String : Any] = ["currentCtr" : self]
        AliyunIdentityManager.sharedInstance().verify(with: verificationToken, extParams: extParams) { [unowned self] response in
            DispatchQueue.main.async {
                var title = ""
                switch response!.code {
                case .ZIMResponseSuccess:
                    title = "人脸对比成功"
                    NetworkManager.manager.request(targetType: .getRealPersonVerificationResult(name: self.nameAes, idCard: self.idCardAes), responseModelType: NetworkDefualtModel.self) { responseModel in
                        if responseModel.code != .successCode {
                            QMUITips.showError(responseModel.msg)
                            return
                        }
                        if let model = YHBaseConfiguration.shared.configModel {
                            if model.relationship {
                                let vc = FQFillInfoViewController(.kinsfolk)
                                vc.idCardAes = self.idCardAes
                                vc.nameAes = self.nameAes
                                self.navigationController?.qmui_pushViewController(vc, animated: true)
                            } else {
                                let vc = FQFillInfoViewController(.baseInfo)
                                vc.idCardAes = self.idCardAes
                                vc.nameAes = self.nameAes
                                self.navigationController?.qmui_pushViewController(vc, animated: true)
                            }
                        }
                    }
                case .ZIMInterrupt:
                    title = "对比失败，用户退出 错误码：\(response!.code.rawValue)"
                case .ZIMNetworkfail:
                    title = "对比失败，网络错误 错误码：\(response!.code.rawValue)"
                case .ZIMResponseFail:
                    title = "对比失败，刷脸失败 错误码：\(response!.code.rawValue)"
                case .ZIMTIMEError:
                    title = "对比失败，设备时间不准确 错误码：\(response!.code.rawValue)"
                default:
                    title = "对比失败，其他错误 错误码：\(response!.code.rawValue)"
                }
                if response!.code != .ZIMResponseSuccess {
                    QMUITips.showError(title)
                }
            }
        }
    }

    override func setupNavigationItems() {
        super.setupNavigationItems()
        title = "人脸识别"
    }
}
